/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package se.fk.klistret.console.ant.types;

import org.apache.tools.ant.taskdefs.condition.IsTrue;
import org.apache.tools.ant.BuildException;

/**
 * 2011-07-11, The comments below are from 2005 and the code is has since
 * upgraded to the Ant 1.8 platform. The patterns and design of this project
 * should not be reused since they are not considered of any quality. The code
 * was written quickly without long term concerns.
 * 
 * 2005, Regular expression match
 * 
 * @author Matthew Young
 * 
 */
public class MatchesType extends IsTrue {
	/**
	 * Regular expression set as a parameter.
	 */
	private String expression;
	/**
	 * Name of the property who's value is matched to the regular expression.
	 */
	private String property_name;

	/**
	 * (Required by Ant IsTrue implementation) Evaluates whether the value for a
	 * property matches the regular expression.
	 * <p>
	 * 
	 * @return <code>boolean</code>, true = matches
	 * 
	 * @throws BuildException
	 *             Thrown if the receiving property name is not set, the regular
	 *             expression is not set, or property has not value set.
	 * 
	 */
	public boolean eval() throws BuildException {
		if (property_name == null)
			throw new BuildException("Property not set as in-parameter");

		if (expression == null)
			throw new BuildException("Expression not set as in-parameter");

		String property_value = getProject().getProperty(property_name);

		if (property_value == null)
			throw new BuildException("Property " + property_name + " not set");

		if (property_value.matches(expression))
			return true;
		else
			return false;
	}

	/**
	 * Sets pattern/regular expression. <b>pattern</b> parameter is reflexed
	 * into this method.
	 * <p>
	 * 
	 * @param expression
	 *            <code>String</code>
	 * 
	 */
	public void setPattern(String expression) {
		this.expression = expression;
	}

	/**
	 * Sets property name. <b>property</b> parameter is reflexed into this
	 * method.
	 * <p>
	 * 
	 * @param name
	 *            <code>String</code>
	 * 
	 */
	public void setProperty(String name) {
		property_name = name;
	}
}
